//登录
$(function() {
	//监听val变化
	$('.log-item input').on('input propertychange focus', function() {
		var _this = $(this);
		if ($(this).val() != '') {
			$('.log-error').html('');
			$(_this).parents('.log-item').addClass('active');
			setTimeout(function() {
				$(_this).siblings('.loginIcon').show();
			}, 200);
		} else {
			$(this).siblings('.loginIcon').removeAttr('style');
		}
	});
	$('.log-item input').on('blur', function() {
		var _this = $(this);
		setTimeout(function() {
			$(_this).parents('.log-item').removeClass('active');
			$(_this).siblings('.loginIcon').removeAttr('style');
		}, 200);
	});
	//清空输入框
	$('.log-item .loginIcon').on('click', function() {
		event.preventDefault();
		$(this).siblings('input').val('');
		$(this).hide();
	});
});

$(document).keyup(function(event) {
	if (event.keyCode == 13) {
		//enter
		login();
	}
});
var pageUrl = window.location.search;
pageUrl = pageUrl.substring(9, pageUrl.length);
function login() {
	$('.log-error').html('');
	var userName = $('#userName').val();
	var pass = $('#password').val();
	if (userName.trim().length == 0 || pass.length == 0) {
		$('.log-error').html('用户名或密码不能为空');
		return false;
	}
	var passmd5 = md5(pass);
	$.ajax({
		type: 'post',
		url: site.apiUrl + '/sUsers/loginByPassword',
		dataType: 'JSON',
		data: {
			account: userName.trim(),
			password: passmd5
		},
		success: function(data) {
			if (data.code == 'rest.success') {
				// if(autoLogin==1){
				//   setCookie("userName",userName,7);
				//   setCookie("userPass",pass,7);
				// }
				// var rs = data.result;
				//token
				window.localStorage.setItem('backstage-access-token', data.result.token);
				//id
				window.localStorage.setItem('backstage-access-id', data.result.id);
				//账号
				window.localStorage.setItem('backstage-access-name', data.result.account);
				//头像
				window.localStorage.setItem('backstage-access-headPicUrl', data.result.userHeadPicUrl);
				//人员昵称
				window.localStorage.setItem('backstage-access-nickName', data.result.nickName);
				//商家ID
				window.localStorage.setItem('backstage-access-businessId', data.result.businessId);
				//角色名称
				window.localStorage.setItem('backstage-access-roleName', data.result.roleName);
				//用户角色Id
				window.localStorage.setItem('backstage-access-roleId', data.result.roleId);
				//人员类型文字
				window.localStorage.setItem('backstage-access-personnelType', data.result.userTypeDisplay);
				//人员类型 1平台管理员，2管理端
				window.localStorage.setItem('backstage-access-personnelstyle', data.result.userType);
				//是否属于市文旅委 1 为是市文旅委
				window.localStorage.setItem('backstage-access-brigade', data.result.brigade);

				//部门id
				window.localStorage.setItem('backstage-access-teamId', data.teamId);

				//菜单权限列表
				if (data.result.menuList != null && data.result.menuList != undefined && data.result.menuList != '') {
					window.localStorage.setItem(
						'backstage-access-permissionList',
						JSON.stringify(data.result.menuList)
					);
        }
				if (pageUrl == '') {
					window.location.href = 'index.html';
				} else {
					window.location.href = pageUrl;
				}
			} else {
				ly.toast(data.desc);
				$('.log-error').html(data.desc);
			}
		}
	});
}
